using UnityEngine;
using UnityEngine.UI;


/****************************************************
// 功能：组队窗口
// | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
// | Copyright (c) 2021-2025 聊来 All rights reserved.
// | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">聊来</a> )
// | @Author: 贯彻己道的云 <18970881148@qq.com>
// | DateTime: 2025/2/22 20:4:8
*****************************************************/

public class GroupWindow : BaseWindow
{
    private Text title;
    private Transform item1;
    private Image avatar1;
    private Text userName1;
    private Button start;
    private Button matching;

    private void Awake()
    {
        title = FindChildTransform(transform, "title").GetComponent<Text>();
        
        item1 = FindChildTransform(transform, "item1");
        avatar1 = item1.Find( "avatar").GetComponent<Image>();
        userName1 = item1.Find("userName").GetComponent<Text>();

        start = FindChildTransform(transform, "start").GetComponent<Button>();
        matching = FindChildTransform(transform, "matching").GetComponent<Button>();
    }

    protected override void InitWindows()
    {
        base.InitWindows();
        
        SetActive(avatar1);
        SetActive(userName1);
        StartCoroutine(SetDownImageToLocal(avatar1, model.userInfo.avatar, "avatar"));
        SetText(userName1, model.userInfo.userName);
        
        start.onClick.AddListener(MatchButtonClick);
        matching.enabled = false;
    }

    protected override void OnOpen(object arg)
    {
        base.OnOpen(arg);
        SetText(title, arg as string);
        MatchController.Instance.groupWindow = this;
    }

    protected override void ClearWindows()
    {
        base.ClearWindows();
        MatchController.Instance.groupWindow = null;
        
        ResetView();
    }

    /// <summary>
    /// 匹配
    /// </summary>
    private void MatchButtonClick()
    {
        audioManager.PlayUIAudio("matchBtnClick");
        SetActive(start.gameObject, false);
        SetActive(matching.gameObject);
        LobbyController.Instance.OnMatch(title.text);
    }

    /// <summary>
    /// 返回上一个窗口
    /// </summary>
    public void OnBackButton()
    {
        SetWindowsState(WindowName.Group, false, ui.CloseHandle);
    }

    /// <summary>
    /// 重置当前页面
    /// </summary>
    public void ResetView()
    {
        SetActive(start.gameObject);
        SetActive(matching.gameObject, false);
    }
}
